package com.yuexun.dhdoctor.module.mythings.ui.fragment;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.baselistener.OnDataBackListener;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.mythings.adapter.ThingsOtherAdapter;
import com.yuexun.dhdoctor.module.mythings.bean.TaskListBean;
import com.yuexun.dhdoctor.module.mythings.bean.TaskListData;
import com.yuexun.dhdoctor.module.mythings.model.impl.ThingModel;
import com.yuexun.dhdoctor.module.mythings.presenter.impl.ThingPresenter;
import com.yuexun.dhdoctor.module.mythings.ui.Act_ThingDetail;
import com.yuexun.dhdoctor.module.mythings.ui.view.IThingsView;
import com.yuexun.dhdoctor.widget.EmptyLayout;
import com.yuexun.dhdoctor.widget.PullToRefresh.FooterLoadingLayout;
import com.yuexun.dhdoctor.widget.PullToRefresh.PullToRefreshBase;
import com.yuexun.dhdoctor.widget.PullToRefresh.PullToRefreshList;

import org.kymjs.kjframe.ui.KJFragment;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/8/14 0014.
 */

public class Frag_CopyMe extends KJFragment implements IThingsView, OnDataBackListener {
    @Bind(R.id.patient_list)
    PullToRefreshList patientList;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;

    private ThingPresenter presenter;
    private ThingsOtherAdapter adapter;
    private ArrayList<TaskListBean> taskListBeans = new ArrayList<>();

    private int nowpage = 1;
    private int pagesize = 999;

    //搜索关键字
    private String key = "";
    private String startDate = "", endDate = "";

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        View mMainView = inflater.inflate(R.layout.frag_things_list, container, false);
        ButterKnife.bind(this, mMainView);
        return mMainView;
    }

    @Override
    protected void initData() {
        super.initData();
        presenter = new ThingPresenter(new ThingModel(), this);
        listViewPreference();

        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
                nowpage = 1;
                ApiForGetList(key);
            }
        });
    }

    /**
     * 网络请求获取列表数据
     */
    private void ApiForGetList(String key) {
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.SESSION_ID));
//        params.put("state", "1");//状态 0-全部；1-审核中；2-已完成；3-已归档
        params.put("title", key);
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        params.put("pageIndex", nowpage + "");
        params.put("pageSize", pagesize + "");
        presenter.getCopyTaskDatas(params);
    }

    /**
     * 初始化ListView样式
     */
    private void listViewPreference() {
        ListView mList = patientList.getRefreshView();
        mList.setDivider(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
        mList.setOverscrollFooter(null);
        mList.setOverscrollHeader(null);
        mList.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
        patientList.setPullLoadEnabled(true);//设置上拉刷新功能关闭
        ((FooterLoadingLayout) patientList.getFooterLoadingLayout()).setNoMoreDataText("无更多数据");

        adapter = new ThingsOtherAdapter(mList, taskListBeans, R.layout.item_things);
        mList.setAdapter(adapter);

        patientList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                nowpage = 1;
                ApiForGetList(key);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                nowpage = nowpage + 1;
                ApiForGetList(key);
            }
        });

        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                Intent intent = new Intent(getActivity(), Act_ThingDetail.class);
                bundle.putString("taskid", taskListBeans.get(position).getTaskId());
                bundle.putInt("type", 3);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    @Override
    public void showData(Bundle bundle) {
        if(bundle != null){
            key = bundle.getString("key");
            startDate = bundle.getString("startDate");
            endDate = bundle.getString("endDate");
            if(bundle.getString("type").equals("2")){
                emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
                nowpage = 1;
                ApiForGetList(key);
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        nowpage = 1;
        ApiForGetList(key);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            key = "";
            nowpage = 1;
            ApiForGetList(key);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void showOtherTaskDatas(TaskListData taskListData) {
        if (patientList.isPullRefreshEnabled()) {
            patientList.onPullDownRefreshComplete();
        }

        if (taskListData.getStatus().equals("200")) {
            if (taskListData.getDatas().size() == 0) {
                patientList.setHasMoreData(false);
            } else {
                if (patientList.isPullLoadEnabled()) {
                    patientList.onPullUpRefreshComplete();
                }
            }

            if (nowpage == 1) {
                taskListBeans.clear();
                taskListBeans = taskListData.getDatas();
            } else {
                taskListBeans.addAll(taskListData.getDatas());
            }
            adapter.refresh(taskListBeans);
        } else {
            ViewInject.toast(taskListData.getMessage());
        }

        if (taskListBeans.size() == 0) {
            emptyLayout.setErrorType(EmptyLayout.NODATA);
        } else {
            emptyLayout.dismiss();
        }
    }
}
